package com.koron.order.patrol.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.patrol.bean.po.PatrolEquipmentPo;
import com.koron.order.patrol.mapper.PatrolEquipmentMapper;
import com.koron.order.patrol.service.PatrolEquipmentService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡视工单-巡视点设备从表(PatrolEquipment)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-26 15:25:48
 */
@Service("patrolEquipmentService")
public class PatrolEquipmentServiceImpl implements PatrolEquipmentService {
    @Autowired
    HttpServletRequest httpServletRequest;
    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, PatrolEquipmentPo patrolEquipment) {

        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);

        return mapper.insertOrUpdate(patrolEquipment);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PatrolEquipmentPo> patrolEquipmentList) {

        Assert.notEmpty(patrolEquipmentList, "巡视设备不能为空");

        List<String> patrolPointIds = patrolEquipmentList.stream().map(PatrolEquipmentPo::getPatrolPointId).distinct().collect(Collectors.toList());

        Assert.notEmpty(patrolPointIds, "巡视点id不能为空");

        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);

        //先根据 巡视点ids删除
        mapper.deleteByPatrolPointIds(patrolPointIds);

        return mapper.insertBatch(patrolEquipmentList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, PatrolEquipmentPo patrolEquipment) {

        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);

        return mapper.insert(patrolEquipment);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<PatrolEquipmentPo> patrolEquipmentList) {

        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);

        return mapper.insertBatch(patrolEquipmentList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, PatrolEquipmentPo patrolEquipment) {
        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);
        return mapper.update(patrolEquipment);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public PatrolEquipmentPo queryById(SessionFactory factory, String id) {

        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<PatrolEquipmentPo> queryList(SessionFactory factory, PatrolEquipmentPo patrolEquipment) {

        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);
        patrolEquipment.setLanguage(httpServletRequest.getHeader("Language"));
        List<PatrolEquipmentPo> list = mapper.queryList(patrolEquipment);

        return list;
    }

    @Override
    @TaskAnnotation("queryByPatrolPointId")
    public List<PatrolEquipmentPo> queryByPatrolPointId(SessionFactory factory, String patrolPointId) {
        PatrolEquipmentPo patrolEquipmentPo = new PatrolEquipmentPo();
        patrolEquipmentPo.setPatrolPointId(patrolPointId);
        return queryList(factory, patrolEquipmentPo);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, PatrolEquipmentPo patrolEquipment, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);
        patrolEquipment.setLanguage(httpServletRequest.getHeader("Language"));
        List<PatrolEquipmentPo> list = mapper.queryList(patrolEquipment);

        return new PageInfo(list);
    }


    /**
     * queryBy patrolPointId
     */
    @Override
    @TaskAnnotation("queryByIds")
    public List<PatrolEquipmentPo> queryByIds(SessionFactory factory, List<String> ids) {

        PatrolEquipmentMapper mapper = factory.getMapper(PatrolEquipmentMapper.class);

        List<PatrolEquipmentPo> list = mapper.queryByIds(ids);

        return list;
    }
}